<?php
class Zend_Template_Model {
    
    private $_controller;
    private $_actions;
    const APPLICATION_NAME = 'Application';
    
    /**
     * Construct
     */
    public function __construct($controller, $actions){
        $this->_controller = $controller;
        $this->_actions = $actions;        
    }
    
    /**
     * render
     * @author tien.nguyen
     */
    public function render(){
        
        global $smarty;
        
        $smarty->assign('controller', $this->_controller);
        $smarty->assign('actions', $this->_actions);
        $smarty->assign('template_path', PATH_STRUCTURE.'/zend-framework/templates/models/main') ;
        
        return $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/model.tpl');
    }
    
    /**
    * Render action
    * @author tien.nguyen
    */
    public function renderAction(){
        $str = "";
        global $smarty;        
        
        foreach($this->_actions as $action){            
            $smarty->assign('mainAction', $action);
            $template = $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/models/action.tpl');
  
            $str .= $template;
        }
        
        return $str;
        
    }
    
    public function readActionTemplate(){
        $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/models/action.tpl');
        return $template; 
    }
    
    public function readTemplate(){
        $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/model.tpl');
        return $template; 
    }
    
    /**
     * Write file
     * @author tien.nguyen
     */
    public function writeFile(){
        
        $template = $this->render();
        
        $path = PATH_PROJECT_GENERAL.'/'.
            NAME_PROJECT_GENERAL.'/application/models/'. 
            $this->_controller->controller_name.'.php';
        
        $hand = fopen($path, 'w');
        fwrite($hand, $template);
        fclose($hand);
    }
}